package protomysql

import (
	"fmt"
	"gitee.com/crack007/goose/gslog"
	"gitee.com/crack007/proxy/src/apputil"
	"strings"

	"gitee.com/crack007/proxy/src/constant"
)

type StmtExecute struct {
}

func (s *StmtExecute) Process(mysql *Mysql) {
	gslog.Debug("%s StmtExecute", mysql.GetSessionId())
	// 上面的 PREPARE 阶段为预处理，只有在 EXECUTE 阶段才是真正开始执行，所以从此处开始计时。
	c := mysql.MysqlClient
	c.profileBegin()
	params := loadMysqlSTMTExecute(c.Stmt.StmtExecute, c.mysqlPackage.PayloadStream, c)
	mysql.MysqlServer.ChangePrepareState(constant.COM_STMT_EXECUTE)
	if len(params) > 0 {
		c.Stmt.FullSql = c.Stmt.PrepareSql
		for _, param := range params {
			c.Stmt.FullSql = strings.Replace(c.Stmt.FullSql, "?", param, 1)
		}
	}

	if c.Stmt.FullSql != "" {
		mysql.DumpSql(fmt.Sprintf("\r\n"+apputil.AddColor("Full: ", constant.COLOR_RED)+"%s\r\n", mysql.MysqlClient.Stmt.FullSql), mysql.MysqlClient.IsSlowQuery())
	}

}
